package com.bluesmart.mia;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.TencentWbShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.util.ToastUtil;

import android.os.Bundle;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ShareActivity extends BaseActivity implements OnClickListener{
	UMSocialService mController;
	String appId = "wxf6f5dfb4e7a3f1e8";
	String appSecret = "20aae0f760949e7fb26863df56c7cb15";
	
	static public Bitmap mScreenShot = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_share);

		((TextView) findViewById(R.id.center)).setText(getString(R.string.share));
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.text0).setOnClickListener(this);
		findViewById(R.id.text1).setOnClickListener(this);
		findViewById(R.id.text2).setOnClickListener(this);
		findViewById(R.id.text3).setOnClickListener(this);
		
		((ImageView)findViewById(R.id.icon)).setImageBitmap(mScreenShot);
		mController = UMServiceFactory.getUMSocialService("com.umeng.share");
		mController.getConfig().closeToast();
	}

	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    super.onActivityResult(requestCode, resultCode, data);
	    /**使用SSO授权必须添加如下代码 */
	    UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ;
	    if(ssoHandler != null){
	       ssoHandler.authorizeCallBack(requestCode, resultCode, data);
	    }
	}
	
	@Override
	public void onClick(View arg0) {
		if(R.id.left == arg0.getId()) finish();
		else
		{
			if(R.id.text0 == arg0.getId())
			{
				UMWXHandler wxHandler = new UMWXHandler(this,appId,appSecret);
				wxHandler.addToSocialSDK();
				
				WeiXinShareContent weixinContent = new WeiXinShareContent();
				weixinContent.setTitle("Mia");
				weixinContent.setTargetUrl("http://www.xiaotuguai.com/");
				if(mScreenShot != null) weixinContent.setShareImage(new UMImage(this,mScreenShot));
				mController.setShareMedia(weixinContent);
				mController.postShare(this, SHARE_MEDIA.WEIXIN, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			else if(R.id.text1 == arg0.getId())
			{
				UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(this, "100424468","c7394704798a158208a74ab60104f0ba");
				qqSsoHandler.addToSocialSDK();  
				
				QQShareContent qqShareContent = new QQShareContent();
				qqShareContent.setTitle("Mia");
		        qqShareContent.setShareContent("Mia");
		        qqShareContent.setTargetUrl("http://www.umeng.com/social");
				if(mScreenShot != null) {
			        qqShareContent.setShareMedia(new UMImage(this,mScreenShot));
				}
				mController.setShareMedia(qqShareContent);
				mController.postShare(this, SHARE_MEDIA.QQ, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			else if(R.id.text2 == arg0.getId())
			{
				UMWXHandler wxCircleHandler = new UMWXHandler(this,appId,appSecret);
				wxCircleHandler.setToCircle(true);
				wxCircleHandler.addToSocialSDK();
				
				CircleShareContent circleMedia = new CircleShareContent();
				circleMedia.setTitle("Mia");
				circleMedia.setTargetUrl("http://www.xiaotuguai.com/");
				if(mScreenShot != null) circleMedia.setShareImage(new UMImage(this,mScreenShot));
				mController.setShareMedia(circleMedia);
				mController.postShare(this, SHARE_MEDIA.WEIXIN_CIRCLE, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
			else if(R.id.text3 == arg0.getId())
			{
				mController.getConfig().setSsoHandler(new SinaSsoHandler());
				mController.getConfig().setSinaCallbackUrl("www.ijiebao.com");
				
				SinaShareContent circleMedia = new SinaShareContent();
				circleMedia.setTitle("Mia");
				if(mScreenShot != null) circleMedia.setShareImage(new UMImage(this,mScreenShot));
				mController.setShareMedia(circleMedia);
				mController.postShare(this, SHARE_MEDIA.SINA, new SnsPostListener()
				{
					@Override
					public void onComplete(SHARE_MEDIA arg0, int eCode,
							SocializeEntity arg2) {
						
						if (eCode == 200) {
							finish();
                        } else {
                            String eMsg = "";
                            if (eCode == -101) {
                                eMsg = "没有授权";
                            }
                            ToastUtil.showToastWithAlertPic("分享失败[" + eCode + "] " + eMsg);
                        }
					}

					@Override
					public void onStart() {
						
					}
				});
			}
		}
	}
}
